﻿@using System.Data
@{
    ViewBag.Title = "Index";
    Layout = null;
    var table = ViewData["sort"] as DataTable;
}

<link href="~/Areas/Admin/style/css/souyin.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<style>
</style>
<link href="~/Scripts/swiper/css/swiper.min.css" rel="stylesheet" />
<script src="~/Scripts/swiper/js/swiper.min.js"></script>
<script src="~/Scripts/basejs.js"></script>
<body style="overflow: hidden;background: #333;">
    <div class="top"></div>
    <div class="content">
        <div class="jz">
            <p class="jz1" id="time">销售时间：</p>
            <p class="jz1">销售单号：@ViewData["orderno"]</p>
            <hr />
            <div class="splist" style="overflow-y: auto; overflow-x: hidden">

            </div>
            <hr />
            <div class="jeinfo">
                <div>
                    <input type="hidden" value="0" id="userid" />
                    <label class="l" id="username">会员</label>
                    <a style="margin-left:0px; background-color: red;display: none;" id="qxhy">取消</a>
                    <a>余额</a>

                    <label class="r" style="color: #666;" id="userye" ye="0">￥0.00 </label>
                </div>
                <div>
                    <label class="l">合计</label>
                    <label class="r" id="hjje" je="0">￥0.00 </label>
                </div>
                <div>
                    <label class="l">折扣</label>
                    <label class="r" id="zhekou" zk="100">100% </label>
                </div>
                <div>
                    <label class="l">应收</label>
                    <label class="r" style="color:#ff6666;" id="yinshou" ys="0">￥0.00 </label>
                </div>
            </div>
            <div class="jsbtn">
                <div onclick="chongzhi()">选择会员</div>
                <div onclick="jiesuan()">结算</div>
            </div>
        </div>

        <div class="jzcz">
            <div onclick="jia()" style="margin-top: 0px;">+</div>
            <div onclick="jian()">-</div>
            <div onclick="showmb(1)">数量N</div>
            <div onclick="showmb(2)">折扣O</div>
            <div onclick="showmb(3)">改价G</div>
            <div onclick="del()">删除D</div>
            <div onclick="chongzhi()">充值R</div>

        </div>
        <div class="product">
            <div class="pro_sort">
                <div class="swiper-container">
                    <div class="swiper-wrapper">
                        <div class="swiper-slide" sid="0">所有分类</div>
                        @{
                            foreach (DataRow row in table.Rows)
                            {
                                <div class="swiper-slide" sid="@row["Id"]">@row["Name"]</div>
                            }
                        }

                    </div>

                </div>
                <script>
                    var swiper = new Swiper('.swiper-container', {

                        slidesPerView: 5,
                        paginationClickable: true,
                        spaceBetween: 20,
                        freeMode: true
                    });
                    $(".swiper-slide:eq(0)").css("background", "#ff6666");
                    $(".swiper-slide:eq(0)").css("color", "#fff");
                </script>
            </div>

            <div class="pro_lst">

            </div>
        </div>
    </div>
    <div class="mbds">

        <div class="shuliang">
            <div class="gbh">
                <div class="gbmb" onclick="gbmb()">
                    ×
                </div>
            </div>
            <table class="edit_table">
                <tr>
                    <td>数量</td>
                    <td><input type="text" id="shuliang" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2">

                        <div class="botton" onclick="editsl()" style="margin-left: 20px;">确定</div>
                        <div class="botton" style="margin-left: 50px;" onclick="gbmb()">取消</div>
                    </td>

                </tr>
            </table>
        </div>
        <div class="shuliang">
            <div class="gbh">
                <div class="gbmb" onclick="gbmb()">
                    ×
                </div>
            </div>
            <table class="edit_table">
                <tr>
                    <td>折扣</td>
                    <td><input type="text" id="tb_zk" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div class="botton" style="margin-left: 20px;" onclick="editzk()">确定</div>
                        <div class="botton" style="margin-left: 50px;" onclick="gbmb()">取消</div>
                    </td>

                </tr>
            </table>
        </div>
        <div class="shuliang">
            <div class="gbh">
                <div class="gbmb" onclick="gbmb()">
                    ×
                </div>
            </div>
            <table class="edit_table">
                <tr>
                    <td>改价</td>
                    <td><input type="text" id="tb_jg" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div class="botton" style="margin-left: 20px;" onclick="editje()">确定</div>
                        <div class="botton" style="margin-left: 50px;" onclick="gbmb()">取消</div>
                    </td>

                </tr>
            </table>
        </div>

        <div class="czmb">
            <div class="gbh"><div class="gbmb" onclick="gbmb()">×</div></div>
            <iframe src="@Url.Action("Index", "UserMoney",new {ck=1})"></iframe>
        </div>
    </div>

    <input type="hidden" id="xzsp" value="0" />
    <input type="hidden" id="money" value="0" />
    <input type="hidden" id="discount" value="0" />
</body>
<script>
    $(function () {
        changecss();
        getprolist(0);
        jsq();
    });
    window.onresize = function () {
        changecss();
    };


    function changecss() {
        var width = ($(window).width() - 440) + "px";
        var height = ($(window).height() - 90) + "px";
        $(".product").css("width", width);
        $(".jz").css("height", height);
        $(".jzcz").css("height", height);
        $(".product").css("height", height);
    }

    $(".swiper-slide").click(function () {
        $(".swiper-slide").css("background", "#fff");
        $(".swiper-slide").css("color", "#000");
        $(this).css("background", "#ff6666");
        $(this).css("color", "#fff");
        var sid = $(this).attr("sid");
        getprolist(sid);

    });

    //获取商品列表
    function getprolist(sid) {
        $.post("@Url.Action("GetPro")", { sid: sid }, function (data) {
            var obj = JSON.parse(data);
            var str = "";

            for (var i = 0; i < obj.length; i++) {
                str += " <a id='cp_" + obj[i].Id + "' img='" + obj[i].Img1 + "' count='" + obj[i].Count + "' name='" + obj[i].Name + "' price='" + obj[i].Price + "' " +
                    " onclick=\"checksp(this.id)\"><img src='" + obj[i].Img1 + "'/> " +
                    "<div class='r'><div class='pro_name'>" + obj[i].Name + "</div>" +
                    "<div class='pro_info'>" +
                    " <label onclick=\"alert(1);\">" + obj[i].Count + "</label><div>库</div>" +
                    "<label>￥" + obj[i].Price + "元</label></div> </div></a>";
            }

            $(".pro_lst").html(str);
        });
    }

    //计时器
    function jsq() {
        setTimeout(jsq, 1000);
        var myDate = new Date();
        var year = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        var mouth = myDate.getMonth(); //获取当前月份(0-11,0代表1月)
        var day = myDate.getDate(); //获取当前日(1-31)
        var hours = myDate.getHours(); //获取当前小时数(0-23)
        var minute = myDate.getMinutes(); //获取当前分钟数(0-59)
        var second = myDate.getSeconds(); //获取当前秒数(0-59)
        var time = year + "年" + mouth + "月" + day + "日" + hours + "时" + minute + "分" + second + "秒";

        $("#time").html("销售时间：" + time);
    }

    function checksp(pid) {
        var temp = 0;
        $(".splist a").each(function () {
            if ($(this).attr("id").split('_')[1] == pid.split('_')[1]) {
                temp = 1;
            }
        });
        if (temp == 0) {
            var str = "<a id='lcp_" + pid.split('_')[1] + "' count='" + $("#" + pid).attr("count") + "' onclick='changelbg(this.id)'>" +
                "<img src='" + $("#" + pid).attr("img") + "'/> " +
                "<div class=\"sp_l_info\">" +
                "   <div class=\"sp_t\">" +
                "       <label>" + ($("#" + pid).attr("name").length > 20 ? $("#" + pid).attr("name").substring(0, 20) : $("#" + pid).attr("name")) + "</label>" +
                "               </div>" +
                "               <div class =\"sp_t\">" +
                "                   <label class=\"spl_l\">￥</label>" +
                "                   <label class=\"spl_l\">" + $("#" + pid).attr("price") + "</label>" +
                "                   <label class=\"spl_l\">元</label>" +
                "                   <label class=\"spl_r\">" + "1" + "</label><label class=\"spl_r\">数量：</label> " +
                "               </div>" +
                "           </div>" +
                "       </a>";
            //---计算金额
            var money = parseFloat($("#money").val());
            money += parseFloat($("#" + pid).attr("price"));
            $("#money").val(money);
            $("#hjje").html("￥" + money);
            $("#hjje").attr("je", money);
            var zk = parseFloat($("#zhekou").attr("zk")) / 100;
            $("#yinshou").html("￥" + zk * money);
            $("#yinshou").attr("ys", (zk * money));
            //---
            $(".splist").append(str);
        } else {
            var zs = $("#lcp_" + pid.split('_')[1]).attr("count");
            var count = $("#lcp_" + pid.split('_')[1] + " label:eq(4)").html();
            if (parseFloat(zs) < (parseFloat(count) + 1)) {
                alert("库存不足！");
                return false;
            }
            $("#lcp_" + pid.split('_')[1] + " label:eq(4)").html((parseFloat(count) + 1));
            //---计算金额
            var money1 = parseFloat($("#money").val());
            money1 += parseFloat($("#" + pid).attr("price"));
            $("#money").val(money1);
            $("#hjje").html("￥" + money1);
            $("#hjje").attr("je", money1);
            var zk1 = parseFloat($("#zhekou").attr("zk")) / 100;
            $("#yinshou").html("￥" + zk1 * money1);
            $("#yinshou").attr("ys", (zk1 * money1));
            //---
        }

    }

    function changelbg(id) {
        $(".splist a").css("background", "#fff");
        $("#" + id).css("background", "#ddd");
        $("#xzsp").val(id);
    }

    //删除左侧商品
    function del() {
        var id = $("#xzsp").val();
        if (id == 0) {
            alert("请选择商品！");
            return false;
        }
        //--计算金额
        var jmoney = parseFloat($("#" + id + " label:eq(2)").html()) * parseFloat($("#" + id + " label:eq(4)").html());
        var money = parseFloat($("#money").val());
        money = money - jmoney;
        $("#money").val(money);
        $("#hjje").html("￥" + money);
        $("#hjje").attr("je", money);
        var zk = parseFloat($("#zhekou").attr("zk")) / 100;
        $("#yinshou").html("￥" + zk * money);
        $("#yinshou").attr("ys", (zk * money));
        //---
        $("#" + id).remove();
    }

    //商品加
    function jia() {
        var id = $("#xzsp").val();
        if (id == 0) {
            alert("请选择商品！");
            return false;
        }
        //--计算金额
        var money1 = $("#" + id + " label:eq(2)").html();
        var money = parseFloat($("#money").val());
        money += parseFloat(money1);
        $("#money").val(money);
        $("#hjje").html("￥" + money);
        $("#hjje").attr("je", money);
        var zk = parseFloat($("#zhekou").attr("zk")) / 100;
        $("#yinshou").html("￥" + zk * money);
        $("#yinshou").attr("ys", (zk * money));

        //--
        var zs = $("#" + id).attr("count");
        var count = $("#" + id + " label:eq(4)").html();
        if (parseFloat(zs) < (parseFloat(count) + 1)) {
            alert("库存不足！");
            return false;
        }
        $("#" + id + " label:eq(4)").html((parseFloat(count) + 1));
    }

    //商品减
    function jian() {
        var id = $("#xzsp").val();
        if (id == 0) {
            alert("请选择商品！");
            return false;
        }
        //--计算金额
        var money1 = $("#" + id + " label:eq(2)").html();
        var money = parseFloat($("#money").val());
        money -= parseFloat(money1);
        $("#money").val(money);
        $("#hjje").html("￥" + money);
        $("#hjje").attr("je", money);
        var zk = parseFloat($("#zhekou").attr("zk")) / 100;
        $("#yinshou").html("￥" + zk * money);
        $("#yinshou").attr("ys", (zk * money));
        //--
        var count = $("#" + id + " label:eq(4)").html();
        if ((parseFloat(count) - 1) == 0) {
            $("#" + id).remove();
        } else {
            $("#" + id + " label:eq(4)").html((parseFloat(count) - 1));
        }
    }

    function gbmb() {
        $(".mbds").hide();

        $(".czmb").hide();
    }

    function chongzhi() {
        $(".mbds").show();
        $(".shuliang").hide();
        $(".czmb").show();

    }


    ///显示操作窗口
    function showmb(index) {

        if (index == 1) {
            var id = $("#xzsp").val();
            if (id == 0) {
                alert("请选择商品！");
                return false;
            }

        }

        index = index - 1;
        $(".mbds").hide();
        $(".mbds").show();
        $(".czmb").hide();
        $(".shuliang").hide();
        $(".shuliang:eq(" + index + ")").show();

    }

    ///修改数量
    function editsl() {

        var id = $("#xzsp").val();
        var jg = parseFloat($("#" + id + " label:eq(2)").html()); //价格
        var oldcount = parseFloat($("#" + id + " label:eq(4)").html()); //数量
        var temp = $("#shuliang").val();
        var temp2 = 0.0;
        if (CheckZNum(temp) == 1) {
            alert("请输入正整数！");
            return false;
        }
        var newcount = parseFloat(temp);
        var money = parseFloat($("#money").val());
        if (newcount == 0) {
            money -= jg * oldcount;
            $("#" + id).remove();
        } else {

            if (newcount > oldcount) {
                // money = money - (oldcount + jg);
                temp2 = jg * (newcount - oldcount);
                money = money + temp2;
            } else if (newcount < oldcount) {
                //money = money - (oldcount + jg);
                temp2 = jg * (oldcount - newcount);
                money = money - temp2;
            } else {
            }
        }
        $("#money").val(money);
        var zk = parseFloat($("#zhekou").attr("zk")) / 100;
        $("#yinshou").html("￥" + zk * money);
        $("#yinshou").attr("ys", (zk * money));
        $("#hjje").html("￥" + money);
        $("#hjje").attr("je", money);
        $("#" + id + " label:eq(4)").html(temp);
        gbmb();
    }

    ///修改折扣
    function editzk() {
        //   var zk = parseFloat($("#zhekou").attr("zk"));
        var temp = $("#tb_zk").val();
        var zk = 0;
        if (CheckZNum(temp) == 1) {
            alert("请输入1-100内的数字");
            return false;
        } else {
            zk = parseFloat(temp);
            if (zk == 0 || zk > 100) {
                alert("请输入1-100内的数字");
                return false;
            } else {
                $("#zhekou").attr("zk", zk);
                $("#zhekou").html(zk + "%");
                var je = parseFloat($("#hjje").attr("je"));
                var money = (je * zk / 100).toFixed(2);
                $("#yinshou").html("￥" + money);
                $("#yinshou").attr("ys", money);
                gbmb();
            }

        }
    }

    ///修改价格
    function editje() {
        var js = $("#tb_jg").val();
        if (CheckMoney(js) == 1) {
            alert("金额输入有误！");
            return false;
        }
        $("#zhekou").attr("zk", 100);
        $("#zhekou").html(100 + "%");
        $("#yinshou").html("￥" + js);
        $("#yinshou").attr("ys", js);
        gbmb();
    }

    ///会员信息
    function userinfo(id, name, money) {
        $("#qxhy").show();
        $("#userid").val(id);
        $("#username").html(name);
        $("#userye").html("￥" + money);
        $("#userye").attr("ye", money);
        gbmb();
    }

    $("#qxhy").click(function () {
        $("#qxhy").hide();
        $("#userid").val(0);
        $("#username").html("会员");
        $("#userye").html("￥0.0");
        $("#userye").attr("ye", "0");
    });

    function jiesuan() {
        if ($(".splist a").size() == 0) {
            alert("请选择商品！");
            return false;
        }
        var orderno = "@ViewData["orderno"]";
        var userid = $("#userid").val();
        var money = $("#yinshou").attr("ys");
        var discount = $("#zhekou").attr("zk");
        var sjmoney = $("#hjje").attr("je");
        var hyye = parseFloat($("#userye").attr("ye"));
        if (userid != 0) {

            if (hyye < money) {
                alert("会员余额不足！");
                return false;
            }
        }
        var count = [];
        var count = [];
        var id = [];
        var je = [];
        $(".splist a").each(function () {

            id.push($(this).attr("id").split('_')[1]);
            count.push($(this).find("label:eq(4)").html());
            je.push($(this).find("label:eq(2)").html());
        });
        $.post("@Url.Action("Addorder")", {
            money: money, userid: userid, spids: id.join(','), count: count.join(','),
            prices: je.join(','), discount: discount, orderno: orderno, sjmoney: sjmoney, hyye: hyye
        }, function (row) {
            if (row == -2) {
                alert("结算成功,请连接小票打印机！");
                location.reload();
            } else {
                if (row > 0) {
                    alert("结算成功！");
                    location.reload();
                }
            }

        });

    }
</script>

